1166D - Cute Sequences - CodeForces Solution


binary search brute force greedy math *2200

Please click on ads to support us..

Python Code:

import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

q = int(input())
ans = []
pow2 = [1]
for _ in range(52):
    pow2.append(2 * pow2[-1])
for _ in range(q):
    a, b, m = map(int, input().split())
    if a == b:
        ans0 = [1, a]
        ans.append(" ".join(map(str, ans0)))
        continue
    l, r = a, a
    sl, sr = a, a
    ok = 1
    x = [a]
    for i in range(2, 51):
        l = sl + 1
        r = sr + m
        sl += l
        sr += r
        x.append(l)
        if l <= b <= r:
            k = i
            break
        elif b < l:
            ok = 0
            break
    if not ok:
        ans0 = "-1"
        ans.append(ans0)
        continue
    for i in range(1, k - 1):
        u = min(m - 1, (b - x[-1]) // pow2[k - i - 2])
        x[i] += u
        for j in range(i + 1, k):
            x[j] += u
            u *= 2
    x[-1] = b
    ans0 = [k] + x
    ans.append(" ".join(map(str, ans0)))
sys.stdout.write("\n".join(ans))

C++ Code:

/*
Problem: 1166D
Date: 17-02-2024 04:45 AM
*/


#include <bits/stdc++.h>

#define FOR(i, n) for(int i = 0; i < n; i++)
#define FORK(i, k, n) for(int i = k; i < n; i++)
#define FORr(i, n) for(int i = n - 1; i >= 0; i--)
#define FORKr(i, k, n) for(int i = n - 1; i >= k; i--)
#define PRINT(a, b) copy((a), (b), ostream_iterator<int>(cout, " "))
#define all(a) a.begin(), a.end()
#define in(a, b) ((b).find(a) != (b).end())
#define sz(a) ((int) (a).size())
#define Mp make_pair
#define PII pair<int, int>
#define LL long long
#define VI vector<int>

using namespace std;

LL a, b, m;

int main() {
    std::ios_base::sync_with_stdio(false);
    int q;
    cin >> q;
    while(q--) {
        cin >> a >> b >> m;
        LL p = 1;
        if(a == b) {
            cout << "1 " << a << endl;
            continue;
        }
        FORK(n, 1, 50) {
            if(b < p * (a + 1)) break;
            if(p * (a + 1) <= b && b <= p * (a + m)) {
                cout << (n + 1) << " " << a << " ";
                vector<int> r(n);
                LL R = b / p - a;
                // cout << "R: " << R << ", ";
                r[n - 1] = 0;
                FOR(i, n - 1) {
                    r[n - i - 2] = ((b % p) >> i) & 1;
                    // cout << +r[n - i - 1] << " ";
                }
                LL term = a;
                LL sum = a;
                FOR(i, n) {
                    term = sum + R + r[i];
                    sum += term;
                    cout << term << " ";
                }
                goto outer;
            }
            p *= 2;
        }
        cout << -1;
        outer:
        cout << endl;
    }
}


Comments

Submit
0 Comments
More Questions

543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins
752. Open the Lock
1535A - Fair Playoff
1538F - Interesting Function
1920. Build Array from Permutation
494. Target Sum
797. All Paths From Source to Target
1547B - Alphabetical Strings
1550A - Find The Array
118B - Present from Lena
27A - Next Test
785. Is Graph Bipartite
90. Subsets II
1560A - Dislike of Threes
36. Valid Sudoku
557. Reverse Words in a String III
566. Reshape the Matrix
167. Two Sum II - Input array is sorted
387. First Unique Character in a String
383. Ransom Note
242. Valid Anagram
141. Linked List Cycle
21. Merge Two Sorted Lists
203. Remove Linked List Elements
733. Flood Fill
206. Reverse Linked List